package com.example.test.conview.view.tool;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.puty.conputyutils.SharedPreferencesHelperUtils;
import com.puty.conputyview.view.attributes.Barcode1dAttributes;
import com.puty.conputyview.view.attributes.Barcode2dAttriutes;
import com.puty.conputyview.view.attributes.BaseAttributes;
import com.puty.conputyview.view.attributes.ImageAttributes;
import com.puty.conputyview.view.attributes.LabelAttributes;
import com.puty.conputyview.view.attributes.RectAttributes;
import com.puty.conputyview.view.attributes.TextAttributes;

/**
 * Created by songgoo on 2017/8/10.
 * 默认标签信息
 */

public class DetaultLabelInfo {
    public static final String ATTRS_LABEL = "attrs_label";
    public static final String ATTRS_TEXT = "attrs_text";
    public static final String ATTRS_BARCODE1D = "attrs_barcode1d";
    public static final String ATTRS_BARCODE2D = "attrs_barcode2d";
    public static final String ATTRS_IMAGE = "attrs_image";
    public static final String ATTRS_RECT = "attrs_rect";

    //默认标签属性
    public static LabelAttributes labelAttributes = new LabelAttributes();

    /**
     * 文本默认属性
     */
    public static TextAttributes textAttributes = new TextAttributes();

    /*
    *一维码属性
    * */
    public static Barcode1dAttributes barcode1dAttributes = new Barcode1dAttributes();

    /*
    * 二维码属性
    * */
    public static Barcode2dAttriutes barcode2dAttriutes = new Barcode2dAttriutes();

    /*
    * 图片属性
    * */
    public static ImageAttributes imageAttributes = new ImageAttributes();

    /*
    * 矩形属性
    * */
    public static RectAttributes rectAttributes = new RectAttributes();

    /*
    * 加载默认属性
    * 从目录中的json文件中
    * */
    public static BaseAttributes load(Context context, String attrType) {
        String json = (String) SharedPreferencesHelperUtils.getSharedPreference(attrType, "");
        if (!TextUtils.isEmpty(json)) {
            Gson gson = new Gson();
            switch (attrType) {
                case ATTRS_LABEL:
                    return gson.fromJson(json, LabelAttributes.class);
                case ATTRS_TEXT:
                    return gson.fromJson(json, TextAttributes.class);
                case ATTRS_BARCODE1D:
                    return gson.fromJson(json, Barcode1dAttributes.class);
                case ATTRS_BARCODE2D:
                    return gson.fromJson(json, Barcode2dAttriutes.class);
                case ATTRS_IMAGE:
                    return gson.fromJson(json, ImageAttributes.class);
                case ATTRS_RECT:
                    return gson.fromJson(json, RectAttributes.class);
            }
        }
        return null;
    }

    /*
    * 保存默认属性
    * 保存到指定目录json文件
    * */
    public static void save(Context context, String attrType) {
        Gson gson = new Gson();
        String attrs = null;
        switch (attrType) {
            case ATTRS_LABEL:
                attrs = gson.toJson(DetaultLabelInfo.labelAttributes);
                break;
            case ATTRS_TEXT:
                attrs = gson.toJson(DetaultLabelInfo.textAttributes);
                break;
            case ATTRS_BARCODE1D:
                attrs = gson.toJson(DetaultLabelInfo.barcode1dAttributes);
                break;
            case ATTRS_BARCODE2D:
                attrs = gson.toJson(DetaultLabelInfo.barcode2dAttriutes);
                break;
            case ATTRS_IMAGE:
                attrs = gson.toJson(DetaultLabelInfo.imageAttributes);
                break;
            case ATTRS_RECT:
                attrs = gson.toJson(DetaultLabelInfo.rectAttributes);
                break;
        }
        SharedPreferencesHelperUtils.put(attrType, attrs);
    }
}
